import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class FahrenheitConverter implements ActionListener {

	private JFrame converterFrame;
	private JPanel converterPanel;
	private JTextField textfieldFahrenheit;
	private JLabel fahLabel, CelLabel;
	private JButton convertButton, Quit;

	public FahrenheitConverter() {

		// Create and set up the window.
		converterFrame = new JFrame();
		converterFrame.setTitle("Convert Fahrenheit to Celsius");
		converterFrame.setSize(400, 600);
		converterFrame.setLocation(200, 200);
		converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create and set up the panel.
		converterPanel = new JPanel(new GridLayout(1, 4));
		converterFrame.getRootPane().setDefaultButton(Quit);
		// Add the components.
		addComponents();

		// Set the default button.
		converterFrame.getRootPane().setDefaultButton(convertButton);

		// Add the panel to the window.
		converterFrame.getContentPane()
				.add(converterPanel, BorderLayout.CENTER);

		// Display the window.
		converterFrame.pack();
		converterFrame.setVisible(true);
	}

	private void addComponents() {
		// Create components.
		textfieldFahrenheit = new JTextField(2);
		fahLabel = new JLabel("Fahrenheit", SwingConstants.LEFT);
		convertButton = new JButton("Convert");
		Quit = new JButton("Quit");
		CelLabel = new JLabel("Celsius           ",
				SwingConstants.LEFT);

		// Listen to events from the Convert button.
		convertButton.addActionListener(this);
		Quit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				converterFrame.dispose();
			}
		});
		
		// Add the components to the container.
		converterPanel.add(textfieldFahrenheit);
		converterPanel.add(fahLabel);
		converterPanel.add(CelLabel);
		converterPanel.add(convertButton);
		converterPanel.add(Quit);

		fahLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		CelLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
	}
	
	public boolean isAlpha(String chaine){
	     int index = 0;
	     if ( chaine == null ){
	        return false;
	     }
	     while ( index < chaine.length()){
	        char carcou = chaine.charAt(index);
	        if ((carcou >='a' && carcou <='z') || (carcou >='A' && carcou <='Z'))
	           index++;
	        else
	           return false;
	     }
	     return true;
	  }
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		try
		{
			int input = Integer.parseInt(textfieldFahrenheit.getText());
			int tempCel = (int) ((input - 32) / 1.8);
			CelLabel.setText(tempCel + " Celsius");
		}
		catch (NumberFormatException e)
		{
			System.out.println("Invalid input");
			JOptionPane jop3 = new JOptionPane();
			jop3.showMessageDialog(null, "Error Msg", "Invalid input", JOptionPane.ERROR_MESSAGE);
			converterFrame.dispose();	
		}
	}
}
